bool defendplenty1(int ignore=-1)
{	
	int difflevel=-1;		
	difflevel=aiGetWorldDifficulty();   
	defendPlan1 =aiPlanCreate("Defend Plan", cPlanDefend); 
	if (defendPlan1 >= 0)   
	{     
	aiPlanAddUnitType(defendPlan1, cUnitTypeMilitary, defendPlanMilNeed, defendPlanMilWant, defendPlanMilMax);    
	aiPlanSetDesiredPriority(defendPlan1, 20);           
	aiPlanSetVariableVector(defendPlan1, cDefendPlanDefendPoint, 0, plenty1Location);   
	aiPlanSetVariableFloat(defendPlan1, cDefendPlanEngageRange, 0, 50.0);   
	aiPlanSetVariableBool(defendPlan1, cDefendPlanPatrol, 0, false);    
	aiPlanSetVariableFloat(defendPlan1, cDefendPlanGatherDistance, 0, 20.0);   
	aiPlanSetInitialPosition(defendPlan1, plenty1Location);  
    aiPlanSetUnitStance(defendPlan1, cUnitStanceDefensive);   
	aiPlanSetVariableInt(defendPlan1, cDefendPlanRefreshFrequency, 0, 5);   
	aiPlanSetNumberVariableValues(defendPlan1, cDefendPlanAttackTypeID, 2, true);  
    aiPlanSetVariableInt(defendPlan1, cDefendPlanAttackTypeID, 0, cUnitTypeUnit);   
	aiPlanSetVariableInt(defendPlan1, cDefendPlanAttackTypeID, 1, cUnitTypeBuilding);   
	aiPlanSetActive(defendPlan1);     
	aiEcho("Creating defend plan");  
	}  
}



int attackPlayerID=-1;
int attackPlan1ID=-1;
int attackPlan2ID=-1;
int difflevel=-1;
int attackRoute1ID=-1;
int attackPath1ID=-1;
int attackRoute2ID=-1;
int attackPath2ID=-1;

void initAttack(int playerID=-1)
{
	//Destroy all previous attacks (if this isn't the player we're already attacking.
   if (playerID != attackPlayerID)
   {
      //Reset the attack player ID.
      attackPlayerID=-1;
      //Destroy any previous attack plan.
      aiPlanDestroy(attackPlan1ID);
      attackPlan1ID=-1;
	  
	  //Destroy our previous attack paths.
      kbPathDestroy(attackPath1ID);
      attackPath1ID=-1;
	  
	  //Destroy our previous attack routes.
      attackRoute1ID=-1;
   }
   
   //Save the player to attack.
   attackPlayerID=playerID;
   
   vector gatherPoint=kbGetBlockPosition("7517");
		
    // Create attack route 1 (land route)
   attackPath1ID=kbPathCreate("Attack Path 1");
   kbPathAddWaypoint(attackPath1ID, kbGetBlockPosition("7518"));
	kbPathAddWaypoint(attackPath1ID, kbGetBlockPosition("7519"));
    //Create attack route 1.
   attackRoute1ID=kbCreateAttackRouteWithPath("Attack Route 1", kbGetBlockPosition("7517"), kbGetBlockPosition("7520"));

	if (attackRoute1ID >= 0)
      kbAttackRouteAddPath(attackRoute1ID, attackPath1ID);
}
   
   //Save the player to attack.
   attackPlayerID=playerID;
   
   vector gatherPoint=kbGetBlockPosition("7517");
		
    // Create attack route 1 (land route)
   attackPath1ID=kbPathCreate("Attack Path 1");
   kbPathAddWaypoint(attackPath1ID, kbGetBlockPosition("7518"));
	kbPathAddWaypoint(attackPath1ID, kbGetBlockPosition("7519"));
    //Create attack route 1.
   attackRoute1ID=kbCreateAttackRouteWithPath("Attack Route 1", kbGetBlockPosition("7517"), kbGetBlockPosition("7520"));

	if (attackRoute1ID >= 0)
      kbAttackRouteAddPath(attackRoute1ID, attackPath1ID);
	  
	// Create attack route 2 (ship route)
   attackPath2ID=kbPathCreate("Attack Path 2");
   kbPathAddWaypoint(attackPath2ID, kbGetBlockPosition("7523"));
	kbPathAddWaypoint(attackPath2ID, kbGetBlockPosition("7522"));
	
	//Create attack route 2.
   attackRoute2ID=kbCreateAttackRouteWithPath("Attack Route 2", kbGetBlockPosition("7524"), kbGetBlockPosition("7521"));

	if (attackRoute2ID >= 0)
      kbAttackRouteAddPath(attackRoute2ID, attackPath2ID);

}

bool setupLandAttack(int playerID=-1)
{
   if (attackPlayerID != playerID)
   {
      initAttack(playerID);
      if (attackPlayerID < 0)
         return(false);
   }
    
   int attackPlan1ID=aiPlanCreate("Attack Player"+attackPlayerID+" Attempt", cPlanAttack);
   if (attackPlan1ID < 0)
      return(false);

   if (aiPlanSetVariableInt(attackPlan1ID, cAttackPlanPlayerID, 0, attackPlayerID) == false)
      return(false);
    
   if (aiPlanSetNumberVariableValues(attackPlan1ID, cAttackPlanTargetTypeID, 2, true) == false)
      return(false);

   //Unit types to attack.
   aiPlanSetVariableInt(attackPlan1ID, cAttackPlanTargetTypeID, 0, cUnitTypeLighthouse);
	aiPlanSetVariableInt(attackPlan1ID, cAttackPlanTargetTypeID, 1, cUnitTypeUnit);
	aiPlanSetVariableInt(attackPlan1ID, cAttackPlanTargetTypeID, 1, cUnitTypeBuilding);
	
	vector gatherPoint=kbGetBlockPosition("7517");

	// Route to take
   aiPlanSetVariableInt(attackPlan1ID, cAttackPlanAttackRouteID, 0, attackRoute1ID);

   aiPlanSetVariableVector(attackPlan1ID, cAttackPlanGatherPoint, 0, gatherPoint);
   aiPlanSetVariableFloat(attackPlan1ID, cAttackPlanGatherDistance, 0, 10.0);

   //Set up the attack route usage pattern.
   aiPlanSetVariableInt(attackPlan1ID, cAttackPlanAttackRoutePattern, 0, cAttackPlanAttackRoutePatternRandom);
    
    
   ///!!!!!!!!!!!!
	switch(difflevel){
        case 0:{    
		      aiPlanAddUnitType(attackPlan1ID, cUnitTypeLogicalTypeLandMilitary, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeSetAnimal, 0, 2, 2);
            break;
        }
        case 1:{     
		      aiPlanAddUnitType(attackPlan1ID, cUnitTypeLogicalTypeLandMilitary, 0, 3, 3);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 6, 6);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 5, 5);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeSetAnimal, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeMythUnit, 0, 1, 1);
            break;
        }
        case 2:{    
		      aiPlanAddUnitType(attackPlan1ID, cUnitTypeLogicalTypeLandMilitary, 0, 3, 3);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 8, 8);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 7, 7);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeSetAnimal, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractSiegeWeapon, 0, 1, 1);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeMythUnit, 0, 1, 1);
            break;
        }
        case 3:{    
		      aiPlanAddUnitType(attackPlan1ID, cUnitTypeLogicalTypeLandMilitary, 0, 4, 4);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 9, 9);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 8, 8);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractSiegeWeapon, 0, 1, 1);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeSetAnimal, 0, 3, 3);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeMythUnit, 0, 2, 2);
            break;
        }
        default:{    
		      aiPlanAddUnitType(attackPlan1ID, cUnitTypeLogicalTypeLandMilitary, 0, 1, 1);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 4, 4);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeSetAnimal, 0, 2, 2);
            break;
        }    
    }
    ///!!!!!!!!!!!!
	
   //Set the initial position.
   aiPlanSetInitialPosition(attackPlan1ID, gatherPoint);
   //Plan requires all need units to work (can be false).
   aiPlanSetRequiresAllNeedUnits(attackPlan1ID, true);
   //Activate the plan.
   aiPlanSetActive(attackPlan1ID);

   //Now, save the attack plan ID appropriately.
   aiPlanSetOrphan(attackPlan1ID, true);
   attackPlan1ID=attackPlan1ID;
}

bool setupWaterAttack(int playerID=-1)
{
   if (attackPlayerID != playerID)
   {
      initAttack(playerID);
      if (attackPlayerID < 0)
         return(false);
   }
    
   int newAttackPlanID=aiPlanCreate("Attack Player"+attackPlayerID+" Attempt", cPlanAttack);
   if (newAttackPlanID < 0)
      return(false);

   if (aiPlanSetVariableInt(newAttackPlanID, cAttackPlanPlayerID, 0, attackPlayerID) == false)
      return(false);

    ///!!!!!!!!!!!!
	vector gatherPoint=kbGetBlockPosition("7524");
    ///!!!!!!!!!!!!
    
   if (aiPlanSetNumberVariableValues(newAttackPlanID, cAttackPlanTargetTypeID, 2, true) == false)
      return(false);

   //Unit types to attack.
   aiPlanSetVariableInt(newAttackPlanID, cAttackPlanTargetTypeID, 0, cUnitTypeLighthouse);
    aiPlanSetVariableInt(newAttackPlanID, cAttackPlanTargetTypeID, 0, cUnitTypeUnit);
	aiPlanSetVariableInt(newAttackPlanID, cAttackPlanTargetTypeID, 1, cUnitTypeBuilding);
	
	// Route to take
   aiPlanSetVariableInt(newAttackPlanID, cAttackPlanAttackRouteID, 0, attackRoute2ID);

   aiPlanSetVariableVector(newAttackPlanID, cAttackPlanGatherPoint, 0, gatherPoint);
   aiPlanSetVariableFloat(newAttackPlanID, cAttackPlanGatherDistance, 0, 40.0);

   //Set up the attack route usage pattern.
   aiPlanSetVariableInt(newAttackPlanID, cAttackPlanAttackRoutePattern, 0, cAttackPlanAttackRoutePatternRandom);
    
    
   ///!!!!!!!!!!!!
	switch(difflevel){
        case 0:{    
		      aiPlanAddUnitType(newAttackPlanID, cUnitTypeLogicalTypeNavalMilitary, 0, 1, 1);
            break;
        }
        case 1:{     
		      aiPlanAddUnitType(newAttackPlanID, cUnitTypeLogicalTypeNavalMilitary, 0, 2, 2);
            break;
        }
        case 2:{    
		      aiPlanAddUnitType(newAttackPlanID, cUnitTypeLogicalTypeNavalMilitary, 0, 3, 3);
            break;
        }
        case 3:{    
		      aiPlanAddUnitType(newAttackPlanID, cUnitTypeLogicalTypeNavalMilitary, 0, 4, 4);
            break;
        }
        default:{    
		      aiPlanAddUnitType(newAttackPlanID, cUnitTypeLogicalTypeNavalMilitary, 0, 1, 1);
            break;
        }    
    }
    ///!!!!!!!!!!!!
	
   //Set the initial position.
   aiPlanSetInitialPosition(newAttackPlanID, gatherPoint);
   //Plan requires all need units to work (can be false).
   aiPlanSetRequiresAllNeedUnits(newAttackPlanID, true);
   //Activate the plan.
   aiPlanSetActive(newAttackPlanID);

   //Now, save the attack plan ID appropriately.
   aiPlanSetOrphan(attackPlan2ID, true);
   attackPlan2ID=newAttackPlanID;
}




rule attackGeneratorLand
   minInterval 180
   active
   group AttackRules
{
   setupLandAttack(1);
}


rule attackGeneratorWater
   minInterval 240
   active
   group AttackRules
{
   setupWaterAttack(1);
}


bool configQueryO( int queryID = -1, int unitType = -1, int action = -1, int state = -1, int player = -1, 
vector center = cInvalidVector, bool sort = false, float radius = -1, bool seeable = false, int areaID = -1 )
{
    if ( queryID == -1)
    return(false);
	
	kbUnitQuerySetPlayerID(queryID, player);
	kbUnitQuerySetUnitType(queryID, unitType);
	kbUnitQuerySetActionType(queryID, action);
	kbUnitQuerySetState(queryID, state);

	kbUnitQuerySetPosition(queryID, center);
	kbUnitQuerySetAscendingSort(queryID, sort);
	kbUnitQuerySetMaximumDistance(queryID, radius);
	
	kbUnitQuerySetAreaID(queryID, areaID);
	kbUnitQuerySetSeeableOnly(queryID, seeable);
	
    return(true);
}

bool configQueryORelation( int queryID = -1, int unitType = -1, int action = -1, int state = -1, int playerRelation = -1, 
vector center = cInvalidVector, bool sort = false, float radius = -1, bool seeable = false, int areaID = -1)
{
    if (queryID == -1)
    {
        aiEcho("Invalid query ID");
        return(false);
	}
	
	kbUnitQuerySetPlayerRelation(queryID, playerRelation);
	kbUnitQuerySetUnitType(queryID, unitType);
	kbUnitQuerySetActionType(queryID, action);
    kbUnitQuerySetState(queryID, state);
	kbUnitQuerySetPosition(queryID, center);
	kbUnitQuerySetAscendingSort(queryID, sort);
	kbUnitQuerySetMaximumDistance(queryID, radius);
	
	kbUnitQuerySetAreaID(queryID, areaID);
	kbUnitQuerySetSeeableOnly(queryID, seeable);
    
    return(true);
}

int findUnitByIndex(int unitTypeID = -1, int index = 0, int state = cUnitStateAlive, int action = -1, int playerID = cMyID, 
vector center = cInvalidVector, float radius = -1, bool seeable = false, int areaID = -1)
{
    static int unitQueryID = -1;
    if (unitQueryID < 0)
    unitQueryID = kbUnitQueryCreate("findUnitByIndex");
    if (index == 0) 
    {
        kbUnitQueryResetResults(unitQueryID);
        configQueryO(unitQueryID, unitTypeID, action, state, playerID, center, true, radius, seeable, areaID ); //sort = true
        kbUnitQueryExecute(unitQueryID);
    }
    int retval = kbUnitQueryGetResult(unitQueryID, index);
    return(retval);
}

bool uOV(int UnitID = -1)
{
	if (UnitID < 0)
	return(false);
    int Owner = kbUnitGetOwner(UnitID);
    if ((kbIsPlayerResigned(Owner) == false) && (kbHasPlayerLost(Owner) == false))
    return(true);
    else
	return(false); 
}

int findClosestUnitTypeByLoc(int Relevance = cPlayerRelationEnemy, int UnitType = -1, int Status = cUnitStateAliveOrBuilding, vector loc = cInvalidVector, int radius = -1, bool seeable = true, bool sameArea = false) 
{
    static int findTargets = -1;
    if (findTargets < 0) 
	{
        findTargets = kbUnitQueryCreate("Closest Target query");
        if (findTargets < 0)
		return(-1);
	}
	kbUnitQuerySetAreaGroupID(findTargets, -1);
    kbUnitQuerySetPlayerRelation(findTargets, Relevance);
	kbUnitQuerySetSeeableOnly(findTargets, seeable);
	kbUnitQuerySetAscendingSort(findTargets, true);
    kbUnitQuerySetUnitType(findTargets, UnitType);
    kbUnitQuerySetState(findTargets, Status);
	kbUnitQuerySetMaximumDistance(findTargets, radius);
	kbUnitQuerySetPosition(findTargets, loc);
	if (sameArea == true)
    kbUnitQuerySetAreaGroupID(findTargets, kbAreaGroupGetIDByPosition(loc));	
    kbUnitQueryResetResults(findTargets);
    int Result = -1;
    int Run = kbUnitQueryExecute(findTargets);
	for (i=0; < Run)
	{
        int CurrentCandidate = kbUnitQueryGetResult(findTargets, i);
		if (uOV(CurrentCandidate) == true)
		{
	        Result = CurrentCandidate;
			break;
		}
	}
    return(Result);
}

int getNumUnits(int unitTypeID = -1, int state = cUnitStateAlive, int action = -1, 
int playerID = cMyID, vector center = cInvalidVector, float radius = -1, bool seeable = false, int areaID = -1)
{
    static int unitQueryID = -1;
    //If we don't have the query yet, create one.
    if (unitQueryID < 0)
	unitQueryID = kbUnitQueryCreate("getNumUnitsQuery");
	
    //Define a query to get all matching units
    if (unitQueryID != -1)
	configQueryO(unitQueryID, unitTypeID, action, state, playerID, center, false, radius, seeable, areaID);
    else
	return(-1);
	
    kbUnitQueryResetResults(unitQueryID);
	int numberFound = kbUnitQueryExecute(unitQueryID);
    return(numberFound);
}

int getNumUnitsByRel(int unitTypeID = -1, int state = cUnitStateAlive, int action = -1, int playerRelation = cPlayerRelationSelf,
vector center = cInvalidVector, float radius = -1, bool seeable = false, int areaID = -1)
{
    static int unitQueryID = -1;
    //If we don't have the query yet, create one.
    if (unitQueryID < 0)
	unitQueryID = kbUnitQueryCreate("getNumUnitsByRelQuery");
	
    //Define a query to get all matching units
    if (unitQueryID != -1)
	configQueryORelation(unitQueryID, unitTypeID, action, state, playerRelation, center, false, radius, seeable, areaID);
    else
	return(-1);
	
    kbUnitQueryResetResults(unitQueryID);
	int numberFound = kbUnitQueryExecute(unitQueryID);
    return(numberFound);
}

rule TroopsAttackLighthouse
minInterval 10
active
group HateScripts
{
    int UnitType = cUnitTypeMilitary;
    int Range = 30;
    int UnitToCounter = cUnitTypeLighthouse;

    int UnitsFound = getNumUnits(UnitType, cUnitStateAlive);
    if ((UnitsFound < 1) || (UnitType == -1) || (UnitToCounter == -1))
    return;

    for (i=0; < UnitsFound)
    {
        int unitID = findUnitByIndex(UnitType, i, cUnitStateAlive);
        if (kbUnitIsType(kbUnitGetTargetUnitID(unitID), UnitToCounter) == true)
        continue;
        vector unitLoc = kbUnitGetPosition(unitID);
        int enemyID = findClosestUnitTypeByLoc(cPlayerRelationEnemy, UnitToCounter, cUnitStateAlive, unitLoc, Range);
        int NumBSelf = getNumUnits(cUnitTypeBuilding, cUnitStateAlive, -1, cMyID, unitLoc, 36.0);
        int NumBAllies = getNumUnitsByRel(cUnitTypeBuilding, cUnitStateAlive, -1, cPlayerRelationAlly, unitLoc, 36.0, true);
        int Combined = NumBSelf + NumBAllies;
        if ((enemyID > -1) && (Combined > 0))
        aiTaskUnitWork(unitID, enemyID);
    }
}

rule  defendMove
minInterval 180
active
{
	defendLand=aiPlanCreate("Land Lighthouse", cPlanDefend);
	if (defendLand >= 0)
	{
		switch(difflevel)
	{
        case 0:{    
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 2, 2);
            break;
        }
        case 1:{     
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 3, 3);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 6, 6);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 5, 5);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeMythUnit, 0, 1, 1);
            break;
        }
        case 2:{    
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 3, 3);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 8, 8);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 7, 7);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractSiegeWeapon, 0, 1, 1);
			  aiPlanAddUnitType(defendLand, cUnitTypeMythUnit, 0, 1, 1);
            break;
        }
        case 3:{    
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 4, 4);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 9, 9);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 8, 8);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractSiegeWeapon, 0, 1, 1);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 3, 3);
			  aiPlanAddUnitType(defendLand, cUnitTypeMythUnit, 0, 2, 2);
            break;
        }
        default:{    
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 1, 1);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 4, 4);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 2, 2);
            break;
        }    
    }
		aiPlanSetDesiredPriority(defendLand, 75);
		aiPlanSetVariableVector(defendLand, cDefendPlanDefendPoint, 0, kbGetBlockPosition("7520"));
		aiPlanSetVariableFloat(defendLand, cDefendPlanEngageRange, 0, 25);
		aiPlanSetUnitStance(defendLand, cUnitStanceAggressive);
		aiPlanSetVariableFloat(defendLand, cDefendPlanRefreshFrequency, 0, 180);
		aiPlanSetActive(defendLand);
	}
}

defendLand=aiPlanCreate("Land Lighthouse", cPlanDefend);
	if (defendLand >= 0)
	{
		switch(difflevel)
	{
        case 0:{    
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 2, 2);
            break;
        }
        case 1:{     
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 3, 3);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 6, 6);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 5, 5);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeMythUnit, 0, 1, 1);
            break;
        }
        case 2:{    
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 3, 3);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 8, 8);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 7, 7);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractSiegeWeapon, 0, 1, 1);
			  aiPlanAddUnitType(defendLand, cUnitTypeMythUnit, 0, 1, 1);
            break;
        }
        case 3:{    
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 4, 4);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 9, 9);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 8, 8);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractSiegeWeapon, 0, 1, 1);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 3, 3);
			  aiPlanAddUnitType(defendLand, cUnitTypeMythUnit, 0, 2, 2);
            break;
        }
        default:{    
		      aiPlanAddUnitType(defendLand, cUnitTypeLogicalTypeLandMilitary, 0, 1, 1);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractInfantry, 0, 4, 4);
			  aiPlanAddUnitType(defendLand, cUnitTypeAbstractArcher, 0, 2, 2);
			  aiPlanAddUnitType(defendLand, cUnitTypeSetAnimal, 0, 2, 2);
            break;
        }    
    }
		aiPlanSetDesiredPriority(defendLand, 75);
		aiPlanSetVariableVector(defendLand, cDefendPlanDefendPoint, 0, kbGetBlockPosition("7520"));
		aiPlanSetVariableFloat(defendLand, cDefendPlanEngageRange, 0, 25);
		aiPlanSetUnitStance(defendLand, cUnitStanceAggressive);
		aiPlanSetVariableFloat(defendLand, cDefendPlanRefreshFrequency, 0, 180);
		aiPlanSetActive(defendLand);
	}
	
	defendNaval=aiPlanCreate("Naval Lighthouse", cPlanDefend);
	if (defendNaval >= 0)
	{
		switch(difflevel)
	{
        case 0:{    
		      aiPlanAddUnitType(defendNaval, cUnitTypeLogicalTypeNavalMilitary, 0, 1, 1);
            break;
        }
        case 1:{     
		      aiPlanAddUnitType(defendNaval, cUnitTypeLogicalTypeNavalMilitary, 0, 2, 2);
            break;
        }
        case 2:{    
		      aiPlanAddUnitType(defendNaval, cUnitTypeLogicalTypeNavalMilitary, 0, 3, 3);
            break;
        }
        case 3:{    
		      aiPlanAddUnitType(defendNaval, cUnitTypeLogicalTypeNavalMilitary, 0, 4, 4);
            break;
        }
        default:{    
		      aiPlanAddUnitType(defendNaval, cUnitTypeLogicalTypeNavalMilitary, 0, 1, 1);
            break;
        }    
    }
		aiPlanSetDesiredPriority(defendNaval, 75);
		aiPlanSetVariableVector(defendNaval, cDefendPlanDefendPoint, 0, kbGetBlockPosition("7521"));
		aiPlanSetVariableFloat(defendNaval, cDefendPlanEngageRange, 0, 25);
		aiPlanSetUnitStance(defendNaval, cUnitStanceAggressive);
		aiPlanSetVariableFloat(defendNaval, cDefendPlanRefreshFrequency, 0, 240);
		aiPlanSetActive(defendNaval);
	}
	
   
   
	vector gatherPoint=kbGetBlockPosition("7517");
	
	
   int maintainPlan1ID=aiPlanCreate("Maintain 2 "+kbGetProtoUnitName(cUnitTypePriest), cPlanTrain);
   if (maintainPlan1ID >= 0)
   {
		//Must set the type of unit to train.
      aiPlanSetVariableInt(maintainPlan1ID, cTrainPlanUnitType, 0, cUnitTypePriest);
      //You can limit the number of units that are ever trained by this plan with this call.
      aiPlanSetVariableInt(maintainPlan1ID, cTrainPlanNumberToTrain, 0, 100);
      //Set the number of units to maintain in the world at one time.
      aiPlanSetVariableInt(maintainPlan1ID, cTrainPlanNumberToMaintain, 0, 2);
      //These train back pretty slowly, giving you time to deal with 'em.
      aiPlanSetVariableInt(maintainPlan1ID, cTrainPlanFrequency, 0, 120);
      //Set a gather point.
      aiPlanSetVariableVector(maintainPlan1ID, cTrainPlanGatherPoint, 0, gatherPoint);
      //Activate the plan.
      aiPlanSetActive(maintainPlan1ID);
   }
   
   
	
   int maintainPlan2ID=aiPlanCreate("Maintain 20 "+kbGetProtoUnitName(cUnitTypeAxeman), cPlanTrain);
   if (maintainPlan2ID >= 0)
   {
		//Must set the type of unit to train.
      aiPlanSetVariableInt(maintainPlan2ID, cTrainPlanUnitType, 0, cUnitTypeAxeman);
      //You can limit the number of units that are ever trained by this plan with this call.
      aiPlanSetVariableInt(maintainPlan2ID, cTrainPlanNumberToTrain, 0, 100);
      //Set the number of units to maintain in the world at one time.
      aiPlanSetVariableInt(maintainPlan2ID, cTrainPlanNumberToMaintain, 0, 20);
      //These train back pretty slowly, giving you time to deal with 'em.
      aiPlanSetVariableInt(maintainPlan2ID, cTrainPlanFrequency, 0, 10);
      //Set a gather point.
      aiPlanSetVariableVector(maintainPlan2ID, cTrainPlanGatherPoint, 0, gatherPoint);
      //Activate the plan.
      aiPlanSetActive(maintainPlan2ID);
   }
   
   switch(difflevel){
        case 0:{    
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractCavalry, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeHero, 0, 2, 2);
            break;
        }
        case 1:{     
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 4, 4);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 4, 4);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractCavalry, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeHero, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeMythUnit, 0, 1, 1);
            break;
        }
        case 2:{    
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 4, 4);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 4, 4);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractCavalry, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeHero, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractSiegeWeapon, 0, 1, 1);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeMythUnit, 0, 1, 1);
            break;
        }
        case 3:{    
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeHero, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 5, 5);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 4, 4);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractCavalry, 0, 3, 3);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractSiegeWeapon, 0, 1, 1);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeMythUnit, 0, 2, 2);
            break;
        }
        default:{    
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractInfantry, 0, 4, 4);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractArcher, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeAbstractCavalry, 0, 2, 2);
			  aiPlanAddUnitType(attackPlan1ID, cUnitTypeHero, 0, 2, 2);
            break;
        }    
    }